package com.lemon.smartsafetynew.ui.fragment.ledger;

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lemon.android.http.LemonRequestParams;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.app.Constants;
import com.lemon.smartsafety.entity.event.http.HttpCallbackEvent;
import com.lemon.smartsafety.http.callback.HandlerCallback;
import com.lemon.smartsafetynew.ui.fragment.base.BaseFragment;
import com.lemon.smartsafety.utils.app.HUDUtil;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafetynew.entity.common.DateTypeEnum;
import com.lemon.smartsafetynew.entity.ledger.LedgerBuildingStatisticsVo;
import com.lemon.smartsafetynew.net.services.SafeLedgerServices;
import com.lemon.smartsafetynew.ui.activity.ledger.LedgerBuildingDetailActivity;
import com.lemon.smartsafetynew.ui.widget.ProgressArcView;
import com.lemon.smartsafetynew.util.common.CommonUtil;
import com.lemon.smartsafetynew.util.constants.KeyConstants;
import com.lemon.smartsafetynew.util.user.AccountUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by Andrew on 2017/9/4.
 */

public class LedgerBuildingBannerFragment extends BaseFragment {

    @Bind(R.id.ledger_title_tv)
    TextView ledgerTitleTv;
    @Bind(R.id.ledger_detail_tv)
    TextView ledgerDetailTv;
    @Bind(R.id.ledger_date_tv)
    TextView ledgerDateTv;
    @Bind(R.id.left_one)
    TextView leftOne;
    @Bind(R.id.left_two)
    TextView leftTwo;
    @Bind(R.id.center_progress)
    ProgressArcView centerProgress;
    @Bind(R.id.center_one)
    TextView centerOne;
    @Bind(R.id.right_one)
    TextView rightOne;
    @Bind(R.id.right_two)
    TextView rightTwo;
    @Bind(R.id.main_layout)
    LinearLayout mainLayout;

    @Override
    protected void getBaseArguments() {

    }

    @Override
    protected int getContentViewId() {
        return R.layout.fragment_ledger_building_banner;
    }

    @Override
    protected void initViews() {
        initMainView();
        getNetData();
    }

    public static LedgerBuildingBannerFragment newInstance() {

        Bundle args = new Bundle();

        LedgerBuildingBannerFragment fragment = new LedgerBuildingBannerFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @OnClick(R.id.ledger_detail_tv)
    public void onViewClicked() {
        UIHelper.showActivity(getmContext(), LedgerBuildingDetailActivity.class);
    }

    private void initMainView() {
        ledgerDateTv.setText(CommonUtil.getNowTimeStr(DateTypeEnum.DAY));
    }

    /**
     * 获取建筑安全统计数据
     */
    private void getNetData() {

        LemonRequestParams params = new LemonRequestParams("");
        params.put("company_id", AccountUtil.getCompanyId());
        params.put("company_code", AccountUtil.getCompanyCode());
        params.put("date", ledgerDateTv.getText().toString().trim());
        params.put("type", DateTypeEnum.DAY.getName().toLowerCase());
        params.put("moreInfo", "0");

        SafeLedgerServices
                .init()
                .getBuildingStatistics(params);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventMainThread(HttpCallbackEvent<LedgerBuildingStatisticsVo> event) {

        if (KeyConstants.URL_GET_SAFE_LEDGER_STATISTICS_OF_BUILDING.equals(event.getTag())) {

            if (HandlerCallback.HttpFlow.START == event.getHttpFlow()) {

            } else if (HandlerCallback.HttpFlow.FINISH == event.getHttpFlow()) {
                if (Constants.Result.SUCCESS == event.getEventResult()) {
                    initContentView(event.getObj());
                } else if (Constants.Result.ERROR == event.getEventResult()) {
                    mHUDUtil.showErrorTip(event.getMsg(), new HUDUtil.CallBack() {
                        @Override
                        public void onCall() {

                        }
                    });
                }
            }
        }
    }

    /**
     * 数据显示
     * @param ledgerBuildingStatisticsVo
     */
    private void initContentView(LedgerBuildingStatisticsVo ledgerBuildingStatisticsVo) {

        leftOne.setText(CommonUtil.avoidNullString(ledgerBuildingStatisticsVo.getTotal_building_num()));
        rightOne.setText(CommonUtil.avoidNullString(ledgerBuildingStatisticsVo.getTotal_item_num()));

        if (ledgerBuildingStatisticsVo.getChecked_item_num() != null &&
                ledgerBuildingStatisticsVo.getTotal_item_num() != null) {

            centerProgress.setPaintTextSize(20);
            centerProgress.setProgress(Integer.valueOf(ledgerBuildingStatisticsVo.getTotal_item_num()),
                    Integer.valueOf(ledgerBuildingStatisticsVo.getChecked_item_num()), true);

        }
    }

    @Override
    public void onPause() {
        super.onPause();
        OkHttpUtils.getInstance().cancelTag(KeyConstants.URL_GET_SAFE_LEDGER_STATISTICS_OF_BUILDING);
    }

}
